﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;

namespace RC1.student
{
    public partial class viewClass : System.Web.UI.Page
    {
        int class_id;

        protected void Page_Load(object sender, EventArgs e)
        {
            class_id = Convert.ToInt32(Request.QueryString["id"]);
            showClassInfo();
        }

        private void showStudents()
        {

        }

        private void showClassInfo()
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();

            var result = (from r in db.classes
                          join ui in db.user_info on r.user_id equals ui.user_id
                          where r.class_id == class_id
                          select new
                          {
                              name = ui.user_info_first_name + " " + ui.user_info_last_name,
                              fak = r.class_name,
                              description = r.class_description
                          }).SingleOrDefault();
            Label1.Text = result.name;
            Label2.Text = result.fak;
            Label3.Text = result.description;
            Label4.Text = "" + getAttendingCount();

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["class_id"] = class_id;
            Server.Transfer("add_assignment.aspx");
        }

        protected void GridviewCommandEventHandler(Object sender, GridViewCommandEventArgs args)
        {
            if (args.CommandName.Equals("viewAssignment"))
            {
                String assignemntID = GridView1.DataKeys[Convert.ToInt32(args.CommandArgument)].Value.ToString();
                int assignment_id = Convert.ToInt32(assignemntID);
                int user_id = Convert.ToInt32(Session["user_id"]);
                Session["assignment_id"] = assignemntID;
                Session["class_id"] = class_id;

                tsu2011db4Entities1 db = new tsu2011db4Entities1();

                var assignment = (from a in db.assignments
                                  where a.assignment_id == assignment_id
                                  select a).SingleOrDefault();

                if (assignment.assignment_deadline <= DateTime.Now)
                {
                    Response.Redirect("view_correction.aspx");
                }

                Response.Redirect("view_assignment.aspx");
            }
        }

        private int getAttendingCount()
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            var attending = (from att in db.attendings
                             where att.class_id == class_id
                             select att);

            return attending.Count();
        }

        protected String getTimeRemaining(int assignement_id)
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            var assignment = (from a in db.assignments
                              where a.assignment_id == assignement_id
                              select a).SingleOrDefault();
            TimeSpan remaingin = assignment.assignment_deadline - DateTime.Now;
            if (DateTime.Now > assignment.assignment_deadline)
            {
                return "Freistin er farin";
            }
            return remaingin.Days + " dagar, " + remaingin.Hours + ":" + remaingin.Minutes + ":" + remaingin.Seconds;
        }

        protected String getCorrectedState(int assignment_id)
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            int user_id = Convert.ToInt32(Session["user_id"]);

            var submission = (from s in db.submissions
                              where s.user_id == user_id
                              where s.assignment_id == assignment_id
                              select s).SingleOrDefault();

            if (submission == null)
            {
                return "Ikki latið inn";
            }
            else
            {
                var corrected = (from c in db.corrected_submission
                                 where c.submission_id == submission.submission_id
                                 select c).SingleOrDefault();
                if (corrected == null)
                {
                    return "Latið inn, Ikki rættað";
                }
                else
                {
                    return "Rættað";
                }
            }
        }
    }
}